/* ====================================================================
 * (#)Header: MapComparator.java
 * (#)Revision: 
 * (#)Date Created: Mar 2, 2007 12:07:22 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.collection;

import java.util.Comparator;
import java.util.Map;

import com.karolak.bean.ComparatorUtils;

/**
 * <code>MapComparator</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class MapComparator implements Comparator<Map<?, ?>> {

	private Object[] keys;

	public MapComparator(Object... keys) {
		this.keys = keys;
	}

	public int compare(Map<?, ?> o1, Map<?, ?> o2) {
		if (o1 == null && o2 == null) {
			return 0;
		}
		if (o1 == null) {
			return -1;
		}
		if (o2 == null) {
			return 1;
		}
		if (getKeys() == null) {
			return 0;
		}
		for (Object key : getKeys()) {
			if (key == null) {
				continue;
			}
			Object p1 = o1.get(key);
			Object p2 = o2.get(key);

			int result = ComparatorUtils.compare(p1, p2);
			if (result != 0) {
				return result;
			}
		}
		return 0;
	}

	protected Object[] getKeys() {
		return keys;
	}
}
